package com.lanou.controller;

import com.lanou.bean.DiaryFinishOrder;
import com.lanou.bean.DiaryPhoto;
import com.lanou.bean.NewDiary;
import com.lanou.bean.User;
import com.lanou.service.DiaryService;
import com.lanou.service.NewDiaryService;
import com.lanou.utils.AjaxResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 18/3/16.
 */
@Controller
public class DiaryController {
    @Resource
    private DiaryService diaryService;
    @Resource
    private NewDiaryService newDiaryService;


    /**
     * 徐鸣 获取日记表需要的参数
     *
     * @param writeDiary   日记内容
     * @param stage        装修阶段
     * @param diaryLabel   日记标签
     * @param date         日期
     * @param diaryPhotos  插入多个日记照片
     * @param finishOrders 插入多个装修订单
     */
    @RequestMapping(value = "/insertDiary")
    public void insertDiary(@RequestParam(value = "writeDiary") String writeDiary,
                            @RequestParam(value = "stage") String stage,
                            @RequestParam(value = "diaryLabel") String diaryLabel,
                            @RequestParam(value = "date") String date,
                            @RequestParam(value = "DiaryPhoto[]") DiaryPhoto[] diaryPhotos,
                            @RequestParam(value = "DiaryFinishOrder[]") DiaryFinishOrder[] finishOrders) {
        diaryService.insertDiary(writeDiary, stage, diaryLabel, date, diaryPhotos, finishOrders);
    }

    /**
     *
     * @param diary_id 评论当条日记的id
     * @param commentContent 评论内容
     * @return 插入是否成功
     * 开发人:赵帅
     */
    @ResponseBody
    @RequestMapping("/insertDiaryComments")
    public AjaxResult insertDiaryComments(@RequestParam("diary_id")Integer diary_id,@RequestParam("commentContent")String commentContent){
       return diaryService.insertDiaryComments(commentContent,diary_id);
   }
    /**
     *
     * @param repeatToUserNick 回复人的昵称
     * @param commentContent 回复的内容
     * @param commenter_id 回复发出评论人的id
     * @return 回复结果
     * 开发人:赵帅
     */
    @ResponseBody
    @RequestMapping("/RepeatContent")
    public AjaxResult insertRepeatContent(@RequestParam("repeatToUserNick")String repeatToUserNick,@RequestParam("commentContent") String commentContent,
                                         @RequestParam("commenter_id")Integer commenter_id){
       return diaryService.insertRepeatContent(repeatToUserNick,commentContent,commenter_id);
   }



    /**
     * 徐鸣 获取添加日记(创建新日记)需要的参数
     * @param diaryTitle  日记标题
     * @param houserType  房屋类型
     * @param area        房屋面积
     * @param city        所在城市
     * @param style       装修风格
     * @param decorateWay 装修方式
     * @param villageName 小区名称
     * @param companyName 装修公司名称
     */
    @RequestMapping(value = ("/inserNewDiary"))
    public void insertNewDiary(@RequestParam("diaryTitle") String diaryTitle,
                               @RequestParam("houserType") String houserType,
                               @RequestParam("area") String area,
                               @RequestParam("city") String city,
                               @RequestParam("style") String style,
                               @RequestParam("decorateWay") String decorateWay,
                               @RequestParam("villageName") String villageName,
                               @RequestParam("companyName") String companyName){
        diaryService.insertNewDiary(diaryTitle,houserType,area,city,style,decorateWay,villageName,companyName);
    }

    /**
     * 徐鸣  查询所有业主日记
     * @return
     */
    @ResponseBody
        @RequestMapping(value = "/selectAllDiary")
    public User selectAllDiary(){
        return diaryService.selectAllDiary();
    }

    /**
     * zfy
     * 装修攻略页面 显示全部的业主日记
     * @return
     */
    @ResponseBody
    @RequestMapping("/getalldiary")
    public List<NewDiary> page(){
        return newDiaryService.getAllDiary();
    }

    /**
     * zfy
     * 1、根据日记的Id 获取该片业主日记的全部内容
     * 2、根据业主日记id 获取该篇日记下的所有装修订单信息
     * @param newDiaryId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getonediary")
    public AjaxResult pageGetOneDiary(@RequestParam("newDiaryId") Integer newDiaryId){
        AjaxResult ajaxResult = new AjaxResult();
        newDiaryService.addClick(newDiaryId);
        NewDiary newDiary = newDiaryService.getDiaryById(newDiaryId);
        List<DiaryFinishOrder> orderList = diaryService.getDiaryFinishOrderId(newDiaryId);
        double sum = diaryService.sumPrice(newDiaryId);
        List list = new ArrayList();
        list.add(newDiary);
        list.add(orderList);
        list.add(sum);

        ajaxResult.setCode("200");
        ajaxResult.setMsg("获取信息成功");
        ajaxResult.setData(list);
        return ajaxResult;

    }

    /**
     * 对日记进行收藏操作
     * @param state
     * @param newDiaryId
     * @return
     */
    @ResponseBody
    @RequestMapping("/addcollect")
    public AjaxResult addClick(@RequestParam("state")Integer state, @RequestParam("newDiaryId")Integer newDiaryId){
        AjaxResult ajaxResult = new AjaxResult();
        if (state == 0){
            newDiaryService.addCollect(newDiaryId);
            ajaxResult.setCode("200");
            ajaxResult.setMsg("收藏成功");
            ajaxResult.setData(null);
        }
        if (state == 1){
            newDiaryService.minusCollect(newDiaryId);
            ajaxResult.setCode("200");
            ajaxResult.setMsg("取消收藏成功");
            ajaxResult.setData(null);
        }
        return ajaxResult;
    }

}
